const api = require('./api.js')
const storage = require('./cache.js')
function login(e) {
  return new Promise((resolve, reject) => {
    wx.login({
      success: function (res) {
        if (res.code) {
          let code = res.code
          wx.getUserInfo({
            success: res => {
              let data = {
                code: code,
                nickName: res.userInfo.nickName,
                avatarUrl: res.userInfo.avatarUrl,
                gid: e||''
              }
              api.POST('index/login', data).then((res) => {
                if (res.error == 0) {
                  console.log(res)
                  storage.put('openid', res.data.openid)
                  wx.setStorageSync('openid', res.data.openid)
                  resolve(res)
                } else {
                  reject(res)
                }
              })
            }
          })
        } else {
          reject(res)
        }
      }
    })
  })
}
module.exports = {
  login: login
}